package com.twu.biblioteca;

import java.io.*;
import java.util.Map;

public class Menu {
    private IOManager ioManager;
    private String welcomeMessage;
    private Map<String, LibraryAction> options;

    private String LIST_BOOKS = "1";


    public Menu(IOManager ioManager, String welcomeMessage, Map<String, LibraryAction> options) {
        this.ioManager = ioManager;
        this.welcomeMessage = welcomeMessage;
        this.options = options;
    }

    private void displayOptions() {
        for (String key : options.keySet()) {
            ioManager.print(key + " - " + options.get(key).toString());
        }
    }

    private void executeUserChoice(String userChoice) throws IOException {
        if (!options.containsKey(userChoice))
            ioManager.print("Select a valid option!");
        else
            options.get(userChoice).performAction();
    }

    public void welcome() throws IOException {
        ioManager.print(welcomeMessage);
        options.get(LIST_BOOKS).performAction();
    }

    public void menu() throws IOException {
        String input = "";
        while(!input.equals("9")) {
            displayOptions();
            input = ioManager.promptUserInput("Enter a number:");
            executeUserChoice(input);
        }
    }
}
